package io.gitee.yxsnake.config.jasypt.config;

import io.gitee.yxsnake.config.jasypt.encrypt.Sm4Encryptor;
import io.gitee.yxsnake.config.jasypt.initializer.CustomContextInitializer;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: snake
 * @create-time: 2024-10-22
 * @description:
 * @version: 1.0
 */
@Slf4j
@Configuration
public class CustomEncryptorConfiguration {


    @Value("${sm4.key:e5153bb7469a8edc1853cc0fc6cd0e08}")
    private String sm4Key;

    private final static String STRING_ENCRYPTOR_BEAN_NAME = "encryptorBean";

    @PostConstruct
    public void init() {
        log.info("------------ embark-config-jasypt-starter start information -----------");
        log.info("config-jasypt-starter");
        log.info("-------------------------------------------------------------");
    }

    @Bean(STRING_ENCRYPTOR_BEAN_NAME)
    public StringEncryptor stringEncryptor(){
        return new Sm4Encryptor(sm4Key);
    }

    @Bean
    public CustomContextInitializer customContextInitializer(){
        return new CustomContextInitializer(STRING_ENCRYPTOR_BEAN_NAME,stringEncryptor());
    }

}
